﻿package Cain.page
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 * PageBook_bn v1.1
	 */
	public class PageBook_bn
	{
		private var next:DisplayObject;
		private var back:DisplayObject;
		private var book:PageBook;
		public function PageBook_bn(_book:PageBook,_next:DisplayObject,_back:DisplayObject)
		{
			book = _book;
			next = _next;
			back = _back;
			book.addEventListener("xml_load_ok", init);
		}
		
		public function init(e=null):void
		{
			next.addEventListener(MouseEvent.CLICK, next_click);
			back.addEventListener(MouseEvent.CLICK, back_click);
			book.addEventListener("start", page_playing);
			back.visible = false;
		}
		
		private function page_playing(e:Event):void 
		{
			next.removeEventListener(MouseEvent.CLICK, next_click);
			back.removeEventListener(MouseEvent.CLICK, back_click);
			book.addEventListener("end",page_playend);
		}
		
		private function page_playend(e:Event):void 
		{
			book.removeEventListener("end", page_playend);
			if (book._now_id<book._pg_num-1) 
			{
				next.visible = true;
				next.addEventListener(MouseEvent.CLICK, next_click);
			}else {
				next.visible = false;
			}
			if (book._now_id>0) 
			{
				back.visible = true;
				back.addEventListener(MouseEvent.CLICK, back_click);
			}else {
				back.visible = false;
			}
			
		}
		
		private function next_click(e:MouseEvent):void 
		{
			if (book._now_id<book._pg_num) 
			{
				if (book.next_page()) 
				{
					next.removeEventListener(MouseEvent.CLICK, next_click);
					book.addEventListener("end", next_end);
				}
			}
		}
		private function back_click(e:MouseEvent):void 
		{
			if (book._now_id>0) 
			{
				if (book.back_page()) 
				{
					back.removeEventListener(MouseEvent.CLICK, back_click);
					book.addEventListener("end", back_end);
				}
			}
		}
		private function next_end(e:Event):void 
		{
			book.removeEventListener("end", next_end);
			next.addEventListener(MouseEvent.CLICK, next_click);
		}
		private function back_end(e:Event):void 
		{
			book.removeEventListener("end",back_end);
			back.addEventListener(MouseEvent.CLICK, back_click);
		}
	}
	
}